/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import main.WinkelApplication;
import model.User;



/**
 *
 * @author Jack Sierkstra
 */
public class UserEdit extends JPanel{
    
    private Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    private String[] Acces= { "Kassiér", "VoorraadBeheerder", "Manager" };
    private TextField usernametext;
    private TextField passwordtext;
    private JComboBox accesleveltext;
    
    public UserEdit(String username) {
        
      super();
      setLayout(new BorderLayout());
      addTitle();
      editpanel(username);
      addButtonPane(username);
    }
    
    
    public void addTitle() {
        JLabel titel = new JLabel("Gebruiker wijzigen", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.getInstance().computeFontSize(2));
        
        JPanel frame = new JPanel();
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));

        frame.add(titel);
        
        titel.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        add(frame, BorderLayout.PAGE_START);
    }
    
    public void addButtonPane(final String username) {
           
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(dim.width, 40));
        
        JButton opslaan = new JButton("Wijzigingen opslaan");
        
        opslaan.setPreferredSize(new Dimension(150,25));
        opslaan.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             
              saveEdit(usernametext.getText(), passwordtext.getText(), accesleveltext.getSelectedIndex()+1,username);            
          }
        });
        
        JButton terug = new JButton("Annuleren");
        
        terug.setPreferredSize(new Dimension(150,25));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             javax.swing.SwingUtilities.getWindowAncestor(UserEdit.this).dispose();
          }
        });

        
        frame.add(opslaan);
        frame.add(terug);
        
        add(frame, BorderLayout.PAGE_END);
         
    }

    public void editpanel(final String username) {
        
        User user = WinkelApplication.getQueryManager().getUser(username);
           
            FormLayout layout = new FormLayout(
                "right:max(40dlu;pref), 3dlu, 40dlu, 7dlu, "
              + "right:max(40dlu;pref), 3dlu, 40dlu",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p");
                
            JPanel panel = new JPanel(layout);
            panel.setBorder(Borders.DIALOG_BORDER);
                       
            
            JLabel Username = new JLabel("Username");
            JLabel password = new JLabel("Password");
            JLabel acceslevel = new JLabel("AccesLevel");
            
            
            Username.setFont(WinkelApplication.computeFontSize(2));
            password.setFont(WinkelApplication.computeFontSize(2));
            acceslevel.setFont(WinkelApplication.computeFontSize(2));
            
            usernametext = new TextField(user.getUsername());
            passwordtext = new TextField(user.getPassword());
            
            accesleveltext = new JComboBox(Acces);
            accesleveltext.setSelectedIndex(user.getAccesLevel()-1);
            //accesleveltext = new TextField(String.valueOf(user.getAccesLevel()));
            
            CellConstraints cc = new CellConstraints();

            panel.add(Username,            cc.xy (1,  3));
            panel.add(usernametext,                 cc.xyw(3,  3, 5));
            panel.add(password,            cc.xy (1,  5));
            panel.add(passwordtext,               cc.xyw(3,  5, 5));
            panel.add(acceslevel,           cc.xy (1, 7));
            panel.add(accesleveltext,                     cc.xyw(3,  7, 5));

            
            add(panel, BorderLayout.CENTER);
        
    }


    public void saveEdit(String username, String password, int AccesLevel,String Username) {
       if(usernametext.getText().isEmpty() || passwordtext.getText().isEmpty()){
           JOptionPane.showMessageDialog(null,
            "Alle velden moeten worden ingevuld!","Fout Melding",
            JOptionPane.INFORMATION_MESSAGE);  
       } else {
        User user = new User(username, password, AccesLevel);
        
        WinkelApplication.getQueryManager().editUser(user, Username);
        
        WinkelApplication.getInstance().showPanel(new view.UserPanel());
        javax.swing.SwingUtilities.getWindowAncestor(UserEdit.this).dispose();
    }
    }
    
    
 
}
